package tech.abing.spider.task.pageprocessor.csrc;

import java.sql.Timestamp;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.bcel.generic.DDIV;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.csrc.CommissionDataEntity;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.UuIdPublic;

public class CommissionContentProcessor implements PageProcessor {

	private static Logger logger = Logger.getLogger(CommissionContentProcessor.class);
	
	private static int record = 0;
	
	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context, List<BasicRequest> queue,
			List<Proccessable> objectContainer) throws Exception {

		Document doc = Jsoup.parse(page.getContent());
		
		String url = page.getRequest().getUrl();
		String infoType = page.getRequest().getParamsByName("infoType").toString();
		record++;
		logger.info("正在解析第"+record+"条数据:");
		CommissionDataEntity cData = new CommissionDataEntity();
		cData.setCreateTime(new Timestamp(System.currentTimeMillis()));
		cData.setInfoType(infoType);
		cData.setUrl(url);
		
		cData.setIndexNo(doc.select("b:containsOwn(索 引 号:)").first().nextSibling().toString());
		cData.setOrgName(doc.select("b:containsOwn(发布机构:)").first().nextElementSibling().ownText());
		cData.setTitle(doc.select("#lTitle").text());
		cData.setReferenceNumber(doc.select("b:containsOwn(文　　号:)").first().nextElementSibling().ownText());
		
		infoType = doc.select("#lSubcat").text();
		if(StringUtils.isNotEmpty(infoType)){
			cData.setInfoType(infoType);
		}
		
		String publishDate = doc.select("b:containsOwn(发文日期:)").first().nextElementSibling().ownText();
		cData.setPublishDate(DateUtils.parseDate(publishDate, new String[]{"yyyy年MM月dd日"}));
		cData.setKeyword(doc.select("b:containsOwn(主 题 词:)").first().nextElementSibling().ownText());
		cData.setContent(doc.select("div.mainContainer").html());
		cData.setId(UuIdPublic.timeStrongID(5));
		objectContainer.add(cData);
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
